package io.wins.vertx.verticle.api;

import io.vertx.core.Future;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.wins.vertx.entity.Role;
import io.wins.vertx.service.RoleService;

public class RoleApiVerticle extends RestApiVerticle {

    public static final String ROLE_REST_ADDRESS = "io.wins.rest.role#%1$s";
    public static final String CREATE_ROLE = "createRole";
    public static final String UPDATE_ROLE = "updateRole";
    public static final String DELETE_ROLE = "deleteRole";
    public static final String LIST_ROLE = "listRole";
    public static final String LOAD_ROLE = "loadRole";
    private static final Logger logger = LoggerFactory.getLogger(RoleApiVerticle.class);

    public final RoleService service;

    public RoleApiVerticle(final RoleService roleService) {
        this.service = roleService;
    }

    @Override
    public void start(final Future<Void> startFuture) throws Exception {
        super.start();
        this.on(String.format(ROLE_REST_ADDRESS, CREATE_ROLE), this::createRole);
        this.on(String.format(ROLE_REST_ADDRESS, LIST_ROLE), this::listRole);
        startFuture.complete();
    }

    private void listRole(final Message<JsonObject> tMessage) {
        final JsonObject param = tMessage.body().getJsonObject("param");
        this.service.listRole(tMessage);
    }

    private void createRole(final Message<JsonObject> tMessage) {
        final JsonObject body = tMessage.body().getJsonObject("body");
        final Role role = new Role(body);
        this.service.createRole(role, tMessage);
    }
}
